package com.jic.manager.controller.rest.conf;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.conf.api.manage.ProductCommentApi;
import com.jic.manager.entity.File;
import com.jic.manager.entity.ProductComment;
import com.jic.conf.request.ProductCommentRequest;
import com.jic.conf.request.rest.UpdateProductCommentRequest;
import com.jic.conf.response.ProductCommentResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.FileService;
import com.jic.manager.service.ProductCommentService;
import com.jic.enums.RedisKey;
import com.jic.order.api.OrderApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: BBC接口 会员收藏列表
 */
@Slf4j
@Api(tags = "H5-评价")
@RestController
@RequestMapping("/apiProductComment")
public class ApiProductCommentController implements ProductCommentApi {

    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Autowired
    private ProductCommentService productCommentService;
    @Resource
    private FileService fileService;
    @Resource
    private OrderApi orderApi;


    /**
     * BBC接口 获取商品评论数量
     * @return
     */
    @ApiOperation(value = "获取个人中心商品评论数量")
    @ResponseBody
    @PostMapping("/getProductCommentCount")
    public RestResult<Long> getProductCommentCount(String token) {
        try {
            String userStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + token);
            if (StringUtils.isEmpty(userStr)) {
                return RestResult.error("4000","登录失效");
            }
            Map member = JSONArray.parseObject(userStr, Map.class);
            Long memberId = Long.valueOf(member.get("id").toString());
            long count = productCommentService.queryCount(new ProductComment() {{
                setMemberId(memberId);
            }});
            return RestResult.success(count);
        } catch (Exception e) {
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }


    @ApiOperation("根据子单编号查询评论")
    @ResponseBody
    @PostMapping("getProductCommentByOrderNo")
    public RestResult<ProductCommentResponse> getProductCommentByOrderNo(@RequestBody ProductCommentRequest productCommentRequest){
        List<ProductComment> productComments = productCommentService.query(new ProductComment() {{
            setOrderNo(productCommentRequest.getOrderNo());
            setProductId(productCommentRequest.getProductId());
        }});
        List<ProductCommentResponse> resList = new ArrayList<>();
        productComments.forEach(productComment -> {
            ProductCommentResponse productCommentResponse = new ProductCommentResponse();
            BeanUtils.copyProperties(productComment,productCommentResponse);
            List<File> files = fileService.query(new File() {{
                setType(0);
                setBusinessId(productComment.getId());
            }});
            productCommentResponse.setFileList(files);
            resList.add(productCommentResponse);
        });
        Map<Integer, List<ProductCommentResponse>> commentResList = resList.stream().collect(Collectors.groupingBy(productCommentResponse -> productCommentResponse.getType()));
        if (commentResList != null){
            List<ProductCommentResponse> productCommentResponses = commentResList.get(1);
            if (CollectionUtils.isNotEmpty(productCommentResponses)){
                ProductCommentResponse productCommentResponse = productCommentResponses.get(0);
                productCommentResponse.setChildren(commentResList.get(2) == null ? new ArrayList<>() : commentResList.get(2));
                return RestResult.success(productCommentResponse);
            }
        }
        ProductCommentResponse productCommentResponse = new ProductCommentResponse();
        productCommentResponse.setChildren(new ArrayList<>());
        return RestResult.success(productCommentResponse);
    }

    @ApiOperation("发表评论/追评")
    @ResponseBody
    @PostMapping("updateProductComment")
    public RestResult<String> updateProductComment(@RequestBody UpdateProductCommentRequest updateProductComment){
        return productCommentService.updateProductComment(updateProductComment);
    }


    @Override
    public RestResult<PageResult<ProductCommentResponse>> getProductCommentPage(ProductCommentRequest productCommentRequest) {
        return null;
    }

    @Override
    public RestResult<ProductCommentResponse> getProductCommentInfo(ProductCommentRequest productCommentRequest) {
        return null;
    }

    @Override
    public RestResult<String> updateProductComment(ProductCommentRequest productCommentRequest) {
        return null;
    }
}
